﻿/*
 * PsTFS is a set of PowerShell commands to manage and use Team Foundation Server. PowerShell 
 * effectiveness is combined with the power of TFS API to maximizes TFS capabilities.
 * 
 * Creator : Choulant Noham
 * Mail : choulant@gmail.com
 * Blog : http://choulant.blogspot.com
 * 
 */

using Microsoft.TeamFoundation.Server;
using Microsoft.TeamFoundation.VersionControl.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using Microsoft.TeamFoundation.Build.Client;
using PsTFS.Objects;

namespace PsTFS.Methodes
{
    /// <summary>
    /// Classe de service permettant de retourner les services de Team Foundation Server
    /// </summary>
    static class MethodService
    {
        /// <summary>
        /// Objet contenant le Version Control Server
        /// </summary>
        private static VersionControlServer _versionControlServer;


        #region IGroupSecurityService

        /// <summary>
        /// Methode retournant le IGroupeSecurityService qui permet de travailer sur la gestion de la sécurité de TFS
        /// </summary>
        /// 
        /// <returns></returns>
        public static IGroupSecurityService IGSS()
        {
            return (IGroupSecurityService)Context.Instance.TFS.GetService(typeof(IGroupSecurityService));
        }

        #endregion IGroupSecurityService

        /// <summary>
        /// Service TFS
        /// </summary>
        /// <returns></returns>
        public static ICommonStructureService CommonStructureService()
        {
            return Context.Instance.TFS.GetService<ICommonStructureService>();

        }

        #region VersionControlServer

        /// <summary>
        /// Permet de récupérer le version contrôle server et de rendre générique la gestion des events
        /// </summary>
        /// 
        /// <returns>Le version control Server</returns>
        public static VersionControlServer VersionControlServer()
        {
            if (_versionControlServer != null)
            {
                if (_versionControlServer.TeamProjectCollection.Name != Context.Instance.TFS.Name)
                {
                    _versionControlServer = null;
                }
            }
            if (_versionControlServer == null)
            {
                _versionControlServer = Context.Instance.TFS.GetService<VersionControlServer>();

                Context.Instance.User = _versionControlServer.TeamProjectCollection.AuthorizedIdentity.DisplayName;
            }

            return _versionControlServer;
        }

        #endregion VersionControlServer
    }
}
